﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace CommunitySystem
{
    public partial class RegisterPositionsControl : UserControl
    {
        private PublicOfficialSelectionService.PublicOfficialSelectionServiceClient client = null;
        private int pagesize = 20;
        private string condition =string.Empty;
        private RPCClass rpc = new RPCClass();
        public RegisterPositionsControl()
        {
            InitializeComponent();
            condition = "p.Status = 4 and r.Status =1 and p.Year=" + DateTime.Now.Year;
            client = new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
            cbPostion.DataContext = rpc;
            cbStatus.DataContext = rpc;
            client.GetRegisterPositionPageCountCompleted += new EventHandler<PublicOfficialSelectionService.GetRegisterPositionPageCountCompletedEventArgs>(client_GetRegisterPositionPageCountCompleted);
            client.GetRegisterPositionCompleted += new EventHandler<PublicOfficialSelectionService.GetRegisterPositionCompletedEventArgs>(client_GetRegisterPositionCompleted);
            client.GetRegisterPositionPageCountAsync(condition, pagesize);
            client.GetRegisterPositionAsync(condition, 0, pagesize);
            
        }

        void client_GetRegisterPositionCompleted(object sender, PublicOfficialSelectionService.GetRegisterPositionCompletedEventArgs e)
        {
            dg.ItemsSource = e.Result;
        }

        void client_GetRegisterPositionPageCountCompleted(object sender, PublicOfficialSelectionService.GetRegisterPositionPageCountCompletedEventArgs e)
        {
            List<int> list = new List<int>();
            for (int i = 0; i < e.Result; i++)
            {
                list.Add(i);
            }
            PagedCollectionView pcv = new PagedCollectionView(list);
            dpager.Source = pcv;
        }

        //void client_GetPublicOfficialSelectionCompleted(object sender, WCFService.GetPublicOfficialSelectionCompletedEventArgs e)
        //{
        //    //PositionWindow win = new PositionWindow(e.Result.FirstOrDefault());
        //    //win.Show();
        //}

        private void description_Click(object sender, RoutedEventArgs e)
        { 
            HyperlinkButton hlb = sender as HyperlinkButton;
            //int id = Convert.ToInt32(hlb.Name);
            //client.GetPublicOfficialSelectionAsync(" p.ID = " + id);
            CommunitySystem.PublicOfficialSelectionService.RegisterPosition rp =  hlb.Tag as CommunitySystem.PublicOfficialSelectionService.RegisterPosition;
            PublicOfficialSelectionWindow psw = new PublicOfficialSelectionWindow(new List<string>(0), false,rp.PositionID,"","0",0,0);
            psw.Show();
        }

        public void BeginReturn()
        {
            
        }

        public string Title
        {
            get;
            set;
        }

        public bool PageLoaded
        {
            get;
            set;
        }

        #region 左右拖动的条
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }
        #endregion


        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            condition = "p.Status = 4  and p.Year=" + DateTime.Now.Year;
            if (!string.IsNullOrEmpty(tbName.Text))
            {
                condition += " and r.Name like '%" + tbName.Text + "%'";
            }
            if (!string.IsNullOrEmpty(tbCardNo.Text))
            {
                condition += " and r.CardNo like '%" + tbCardNo.Text + "%'";
            }
            if (rpc.PositionID != -1)
            {
                condition += " and p.ID=" + rpc.PositionID;
            }
            if (rpc.Status != "Null" && !string.IsNullOrEmpty(rpc.Status))
            {
                condition += " and r.Status=" + rpc.Status;
            }
            client.GetRegisterPositionPageCountAsync(condition, pagesize);
            client.GetRegisterPositionAsync(condition, 0, pagesize);
        }

        private void dgPosition_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            HyperlinkButton hlbD = dg.Columns[10].GetCellContent(e.Row).FindName("description") as HyperlinkButton;
            HyperlinkButton hlbR = dg.Columns[9].GetCellContent(e.Row).FindName("register") as HyperlinkButton;
            int index = e.Row.GetIndex();
            var entity = (dg.ItemsSource as  ObservableCollection<CommunitySystem.PublicOfficialSelectionService.RegisterPosition>)[index];
            if (hlbD != null)
            {
                hlbD.Tag = entity;
            }
            if (hlbR != null)
            {
                hlbR.Tag = entity;
            }
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void register_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hlb = sender as HyperlinkButton;
            CommunitySystem.PublicOfficialSelectionService.RegisterPosition rp = hlb.Tag as CommunitySystem.PublicOfficialSelectionService.RegisterPosition;
            //new PersonnelInfoWindow(rp.PositionID, rp.Name, rp.CardNo, 0).Show();
        }


        
    }

    public class RPCClass: INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string PropName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(PropName));
            }
        }

        private int _PositionID = -1;

        public int PositionID
        {
            get { return _PositionID; }
            set
            {
                _PositionID = value;
                this.OnPropertyChanged("PositionID");
            }
        }

        private string _Status;

        public string Status
        {
            get { return _Status; }
            set
            {
                _Status = value;
                this.OnPropertyChanged("Status");
            }
        }
        
        
    }
}
